package middleware

import (
	"fmt"
	"lib/mall/v1/lib"
	"lib/mall/v1/plugins"
	"net/http"
)

// ErrorMiddleware 错误处理
func ErrorMiddleware(ctx *lib.Context) {
	fmt.Println("===start ErrorHandle====")
	defer func() {
		err := recover()
		//fmt.Printf("%T, %v,\n", err, err)
		var message string
		switch err.(type) {
		case error:
			message = err.(error).Error()
		case string:
			message = err.(string)
		default:
			fmt.Printf("type default,\n")
		}
		if len(message) > 0 {
			plugins.NewResponse().Json(ctx.Context, http.StatusOK, false, nil, message, nil)
		}
	}()
	ctx.Next()
	fmt.Println("===end ErrorHandle====")

}
